cmake_minimum_required(VERSION 3.1.3)
project("file-server")
set(CMAKE_CXX_STANDARD 17)

set(src_path "${CMAKE_CURRENT_SOURCE_DIR}/source")
file(GLOB src_files "${src_path}/*.cc")
set(proto_path "${CMAKE_CURRENT_SOURCE_DIR}/../proto")
set(proto_file_names "file.proto" "base.proto")
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -O0 -ggdb")
foreach (proto_file_name ${proto_file_names})
    string(REPLACE ".proto" ".pb.cc" proto_cc ${proto_file_name})
    if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${proto_cc})
        add_custom_command(
            PRE_BUILD
            COMMAND protoc
            ARGS --cpp_out=${CMAKE_CURRENT_BINARY_DIR} --proto_path=${proto_path} "${proto_path}/${proto_file_name}"
            DEPENDS "${proto_path}/${proto_file_name}"
            OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}"
            COMMENT "生成proto框架代码文件: ${proto_cc}"
        )
    endif()
    list(APPEND proto_ccs "${CMAKE_CURRENT_BINARY_DIR}/${proto_cc}")
endforeach()


add_executable(file_server ${src_files} ${proto_ccs})

# 测试客户端
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/test/file_client.cc")
    message(STATUS "Found test client files, adding test client executable.")
    file(GLOB file_client_file "${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc")
    add_executable(file_client ${file_client_file} ${proto_ccs})
    target_link_libraries(file_client brpc gtest leveldb gflags ssl curl crypto protobuf etcd-cpp-api cpprest fmt jsoncpp spdlog fmt)
endif()


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../common)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

target_link_libraries(file_server brpc leveldb gflags ssl curl crypto protobuf etcd-cpp-api cpprest fmt jsoncpp spdlog fmt)

install(TARGETS file_server RUNTIME DESTINATION bin)